package com.zpark.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.it.mapper.AddressMapper;
import com.zpark.it.pojo.Address;
import com.zpark.it.service.AddressService;
import com.zpark.it.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: AddressServiceImpl
 * @Author Charles
 * @Package com.zpark.it.service.impl
 * @Date 2023/7/20 16:57
 */
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    /**
     * 根据用户id查询地址数据
     * @param userId
     * @return
     */
    @Override
    public R list(Integer userId) {
        //1、封装查询参数
        QueryWrapper<Address> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Address> addressList = addressMapper.selectList(queryWrapper);
        //2、结果封装
        R ok = R.ok("查询成功", addressList);
        log.info("结果：{}","查询成功");
        return ok;
    }

    /**
     * 插入地址数据，插入成功以后要返回新的数据集合
     * @param address 地址数据已经校验完毕
     * @return 数据集合
     */
    @Override
    public R save(Address address) {
        //1、插入数据
        int rows = addressMapper.insert(address);
        if (rows==0){
            log.info("结果为：{}","地址插入失败");
        }
        //2、复用查询业务
        return list(address.getUserId());
    }

    /**
     * TODO
     * 1、定义接受参数的param并且添加参数校验注解
     * 2、编写controller
     * 3、编写service
     * 4、编写mapper
     * 根据用户id删除地址数据
     * @param id 地址id
     * @return
     */
    @Override
    public R remove(Integer id) {
        int rows = addressMapper.deleteById(id);
        if (rows==0){
            log.info("结果为：{}","地址删除失败");
            return R.fail("删除地址数据失败");
        }
        log.info("结果为：{}","地址删除成功");
        return R.ok("地址删除成功");
    }
}
